#!/bin/bash
# This is the script for running SPECcpu
###SHELLPACK preamble speccpu2017-bench 1.0.5

SPECCPU_DATA_SIZE=ref
SPECCPU_ITERATIONS=3
SPECCPU_TESTS=specspeed

install-depends dmidecode gcc gcc-c++ gcc-fortran

###SHELLPACK parseargBegin
###SHELLPACK parseargInstall
###SHELLPACK parseargParam --input-data-size	SPECCPU_DATA_SIZE
###SHELLPACK parseargParam --iterations		SPECCPU_ITERATIONS
###SHELLPACK parseargParam --tests		SPECCPU_TESTS
###SHELLPACK parseargParam --parallel		SPECCPU_PARALLEL
###SHELLPACK parseargYes   --build		SPECCPU_BUILDONLY
###SHELLPACK parseargEnd
###SHELLPACK monitor_hooks

# Detect bitness
case `uname -m` in
	i?86)
		RUNBITS=32
		;;
	*)
		RUNBITS=64
		;;
esac

# Detect whether it is reportable
REPORTABLE=--reportable
if [ "$TESTS" != "specspeed" -a "$TESTS" != "specrate" ]; then
	if [ "$SPECCPU_ITERATIONS" = "1" -o "$SPECCPU_DATA_SIZE" != "ref" ]; then
		REPORTABLE=--noreportable
	fi
fi

###SHELLPACK check_install_required speccpu2017-${VERSION}
###SHELLPACK init_complete

# Prepare system
cd $SHELLPACK_SOURCES/speccpu2017-${VERSION}-installed > /dev/null || die Failed to cd to speccpu
[ ! -e shrc ] && die No speccpu2017 shrc script
[ -e result ] && rm -rf result
. shrc
mkdir -p config

# Generate SPEC configurations
if [ ! -e  config/gen-m${RUNBITS}.cfg ]; then
	echo Auto-generating speccpu 2017 file
	generate-speccpu2017.sh			\
		--spectype $SPECCPU_TESTS	\
		--bitness ${RUNBITS}		\
		--parallel $SPECCPU_PARALLEL	\
		--iterations $SPECCPU_ITERATIONS > config/gen-m${RUNBITS}.cfg || die Failed to generate spec config file
fi

# For parallel speed runs, only run fpspeed and not intspeed. According to the
# documentation, only one intspeed benchmark (657.xz_s) supports openmp so
# it's a waste of time to run the full suite.
if [ "$SPECCPU_TESTS" = "specspeed" ]; then
	if [ $SPECCPU_PARALLEL -gt 1 ]; then
		echo WARNING:
		echo WARNING: Limiting parallelised specspeed test to fpspeed as only one
		echo WARNING: intspeed benchmark supports openmp parallelisation.
		echo WARNING:
		SPECCPU_TESTS="fpspeed"
	fi
fi

# Prebuild everything if requested
if [ "$SPECCPU_BUILDONLY" = "yes" ]; then
	mmtests_activity source-install
	echo Prebuilding $SPECCPU_TESTS
	$MMTESTS_HUGECTL runcpu --config gen-m${RUNBITS}.cfg	\
		$REPORTABLE					\
		--tune base					\
		--size $SPECCPU_DATA_SIZE			\
		--iterations $SPECCPU_ITERATIONS		\
		--action=build $SPECCPU_TESTS || die Failed to build spec

	echo Speccpu 2017 build-only for $SPECCPU_TESTS complete
	exit $SHELLPACK_SUCCESS
fi

# Remove stack size limits or cam4_s will fail. A fixed size could be used
# but xz and cam4_s disagree on what that size should be when parallelised.
ulimit -s unlimited

# Run the benchmark
mmtests_activity runcpu
monitor_pre_hook $LOGDIR_RESULTS $P
$MMTESTS_HUGECTL runcpu --config gen-m${RUNBITS}.cfg	\
	$REPORTABLE					\
	--tune base					\
	--size $SPECCPU_DATA_SIZE			\
	--iterations $SPECCPU_ITERATIONS		\
	$SPECCPU_TESTS
RETVAL=$?
monitor_post_hook $LOGDIR_RESULTS $P
mv result/* $LOGDIR_RESULTS/
gzip $LOGDIR_RESULTS/*.log

exit $RETVAL
